package com.examination.controller.common_module;

import com.examination.bean.Selection;
import com.examination.controller.BaseServletController;
import com.examination.service.ISelectionAdminService;
import com.examination.service.impl.SelectionAdminService;
import com.examination.utils.Constants;
import com.examination.utils.PageInfo;
import com.examination.utils.ResponseResult;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择题库模块
 */
@WebServlet(name = "/SelectionAdmin", urlPatterns = "/SelectionAdmin")
public class SelectionAdmin extends BaseServletController {
    ISelectionAdminService selectionAdminService = new SelectionAdminService();


    /**
     * 添加
     * @param request
     * @param response
     * @return
     */
    public String addSelection(HttpServletRequest request, HttpServletResponse response){
        Selection selection = new Selection();
        selection = getObjectByRequest(selection,request);
        //組裝选项json格式
        Map<String, String[]> requestParamMap = request.getParameterMap();
        String[] choiceInfoArry = requestParamMap.get("choiceInfo");
        if(null != choiceInfoArry && choiceInfoArry.length > 0){
            JSONObject jsonObject = new JSONObject();
            for (int i = 0; i < choiceInfoArry.length; i++) {
                jsonObject.put(((char)(65+i))+"",choiceInfoArry[i]);//A,B,C,D...序号
            }
            selection.setChoiceInfo(jsonObject.toString());
        }
        String row = selectionAdminService.addSelection(selection);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState("200");
        JSONObject resResultJson = new JSONObject(responseResult);
        rendDate("application/json;charset=utf8", resResultJson.toString(), response);
        return Constants.RESULT_SUCCESS;
    }
    /**
     * 获得选择题库题目列表
     *
     * @param request
     * @param response
     * @return {
     * "state": 200,
     * "msg": "success",
     * "list": [
     * {
     * "id": 12,
     * "type": "multi_choice",
     * "content": "中国是世界人口最多国家？",
     * "choice_info": {
     * "A": 100,
     * "B": 200,
     * "C": 300,
     * "D": 400,
     * "E": 500
     * }
     * },
     * {
     * "id": 12,
     * "type": "multi_choice",
     * "content": "中国是世界人口最多国家？",
     * "choice_info": {
     * "A": 100,
     * "B": 200,
     * "C": 300,
     * "D": 400,
     * "E": 500
     * }
     * }
     * ]
     * }
     */
    public String getSelectionListByPage(HttpServletRequest request, HttpServletResponse response) {
        int totalPageNum = request.getParameter("totalPageNum") == null ? 0 : Integer.valueOf(request.getParameter("totalPageNum") + "");
        int curPage = request.getParameter("curPage") == null ? 0 : Integer.valueOf(request.getParameter("curPage") + "");
        int pageSize = request.getParameter("pageSize") == null ? 0 : Integer.valueOf(request.getParameter("pageSize") + "");
        PageInfo pageInfo = new PageInfo(curPage,pageSize);
        pageInfo  = selectionAdminService.getSelectionListByPage(pageInfo);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState("200");
        responseResult.setMsg("success");
        responseResult.setData(pageInfo);
        //TODO 将对象转成json格式
        JSONObject resResultJson = new JSONObject(responseResult);
        System.out.println(resResultJson.toString());
        /*String selectionListJson = "{\n" +
                "  \"state\": 200, \n" +
                "  \"msg\": \"success\", \n" +
                "  \"list\": [\n" +
                "    {\n" +
                "      \"id\": 12, \n" +
                "      \"type\": \"multi_choice\", \n" +
                "      \"content\": \"中国是世界人口最多国家？\", \n" +
                "      \"choice_info\": {\n" +
                "        \"A\": 100, \n" +
                "        \"B\": 200, \n" +
                "        \"C\": 300, \n" +
                "        \"D\": 400, \n" +
                "        \"E\": 500\n" +
                "      }\n" +
                "    }, \n" +
                "    {\n" +
                "      \"id\": 12, \n" +
                "      \"type\": \"multi_choice\", \n" +
                "      \"content\": \"中国是世界人口最多国家？\", \n" +
                "      \"choice_info\": {\n" +
                "        \"A\": 100, \n" +
                "        \"B\": 200, \n" +
                "        \"C\": 300, \n" +
                "        \"D\": 400, \n" +
                "        \"E\": 500\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}";*/
        //1.从数据库查选择库记录列表
        rendDate("application/json;charset=utf8", resResultJson.toString(), response);
        return Constants.RESULT_SUCCESS;
    }
}
